from rest_framework.viewsets import GenericViewSet
from rest_framework_simplejwt.views import TokenViewBase
from rest_framework_simplejwt.exceptions import InvalidToken, TokenError
from rest_framework import status

from projects.base.response import APIResponse
from projects.base.mixin import ListModelMixin, RetrieveModelMixin
from .serializers import TokenLoginSerializer, ChatUserSerializer
from .models import UserProfile


class TokenLoginView(TokenViewBase):
    """登录接口"""
    serializer_class = TokenLoginSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)

        try:
            serializer.is_valid(raise_exception=True)
        except TokenError as e:
            raise InvalidToken(e.args[0])
        return APIResponse(result=serializer.validated_data, status=status.HTTP_200_OK)


class TokenLogoutView():
    ...


class ChatUserViewSet(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    """注册用户的列表与详情"""
    lookup_field = "uid"
    queryset = UserProfile.objects
    serializer_class = ChatUserSerializer

